//
//  CompanyCertificationUploadPhotoViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/8/25.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "CompanyCertificationUploadPhotoViewController.h"
#import "PersonalDataVCtrol.h"
#import "IGUploadImageApi.h"
#import "IGIdentityAuthenApi.h"
#import "IGUploadImageApi.h"
#import "PhotoUpLoadAgreementViewController.h"
#import "UserIdentifyViewController.h"
#import "UserUNidentyViewController.h"

@interface CompanyCertificationUploadPhotoViewController (){
    NSString *img1;
    NSString *img2;
    NSString *img3;
}
@property (weak, nonatomic) IBOutlet UIImageView *licenseImageView;
@property (weak, nonatomic) IBOutlet UIButton *licenseBtn;
@property (weak, nonatomic) IBOutlet UILabel *licenseLabel;
@property (weak, nonatomic) IBOutlet UIImageView *frontImageView;
@property (weak, nonatomic) IBOutlet UIButton *frontBtn;
@property (weak, nonatomic) IBOutlet UILabel *frontLabel;
@property (weak, nonatomic) IBOutlet UIImageView *reverseImageView;
@property (weak, nonatomic) IBOutlet UIButton *reverseBtn;
@property (weak, nonatomic) IBOutlet UILabel *reverselabel;

@property (strong, nonatomic) IBOutlet UIButton *submit;
@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, strong) NSMutableArray *imageUrls;
@property (nonatomic, strong) IGIdentityAuthenApi *identyApi;
@property (nonatomic, strong) IGUploadImageApi *licenseImageApi;
@property (nonatomic, strong) IGUploadImageApi *frontImageApi;
@property (nonatomic, strong) IGUploadImageApi *reverseImageApi;
@property (nonatomic, strong) IGUploadImageApi *zhengImageApi;

@end

@implementation CompanyCertificationUploadPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"用户认证";
    self.title = NSLocalizedString(@"Identity authentication", nil);
    _images = [NSMutableArray array];
    _imageUrls = [NSMutableArray array];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
    if (self.type==0) {
        [self UI1hidden];
        [self setUI2];
        [self setUI3];
    }
    if (self.type==1){
       [self setUI1];
        [self setUI2];
       [self setUI3];
    }
    _imageUrls = [NSMutableArray new];
    _images = [NSMutableArray new];
    
}

-(void)setUI1{
    _licenseImageView.frame=CGRectMake(GSCALE(35), GSCALE(10), GSCALE(300), GSCALE(150));
    
    _licenseBtn.size=CGSizeMake(GSCALE(60), GSCALE(60));
    _licenseBtn.centerX=self.view.centerX;
    _licenseBtn.bottom=_licenseLabel.top-GSCALE(32);
    _licenseBtn.top=_licenseImageView.top+GSCALE(30);
    
    _licenseLabel.size=CGSizeMake(GSCALE(142), GSCALE(17));
    _licenseLabel.centerX=self.view.centerX;
    _licenseLabel.top= _licenseBtn.bottom+GSCALE(32);
}
-(void)UI1hidden{
    _licenseImageView.hidden=YES;
    _licenseBtn.hidden=YES;
    _licenseLabel.hidden=YES;
}
-(void)setUI2{
    if (self.type==0) {
        _frontImageView.frame=CGRectMake(GSCALE(35), GSCALE(60), GSCALE(300), GSCALE(170));
        
        _frontBtn.size=CGSizeMake(GSCALE(60), GSCALE(60));
        _frontBtn.centerX=self.view.centerX;
        _frontBtn.bottom=_frontImageView.top-GSCALE(36);
        _frontBtn.top=_frontImageView.top+GSCALE(40);
        
        _frontLabel.size=CGSizeMake(GSCALE(142), GSCALE(17));
        _frontLabel.centerX=self.view.centerX;
        _frontLabel.top=_frontBtn.bottom+GSCALE(36);
    }else{
    _frontImageView.size=CGSizeMake(GSCALE(300), GSCALE(150));
    _frontImageView.centerX=self.view.centerX;
    _frontImageView.top=_licenseImageView.bottom+GSCALE(10);
    
    _frontBtn.size=CGSizeMake(GSCALE(60), GSCALE(60));
    _frontBtn.centerX=self.view.centerX;
    _frontBtn.bottom=_frontLabel.top-GSCALE(36);
    _frontBtn.top=_frontImageView.top+GSCALE(30);
    
    _frontLabel.size=CGSizeMake(GSCALE(142), GSCALE(17));
    _frontLabel.centerX=self.view.centerX;
    _frontLabel.bottom=_frontBtn.bottom+GSCALE(32);
    }
    
}
-(void)setUI3{
    if (self.type==0) {
        _reverseImageView.size=CGSizeMake(GSCALE(300), GSCALE(170));
        _reverseImageView.centerX=self.view.centerX;
        _reverseImageView.top=_frontImageView.bottom+GSCALE(60);
        
        _reverseBtn.size=CGSizeMake(GSCALE(60), GSCALE(60));
        _reverseBtn.centerX=self.view.centerX;
        _reverseBtn.bottom=_reverselabel.top-GSCALE(36);
        _reverseBtn.top=_reverseImageView.top+GSCALE(40);
        
       _reverselabel.size=CGSizeMake(GSCALE(142), GSCALE(17));
        _reverselabel.centerX=self.view.centerX;
        _reverselabel.bottom=_reverseBtn.bottom+GSCALE(36);
    }else{
    _reverseImageView.size=CGSizeMake(GSCALE(300), GSCALE(150));
    _reverseImageView.centerX=self.view.centerX;
    _reverseImageView.top=_frontImageView.bottom+GSCALE(10);
    
    _reverseBtn.size=CGSizeMake(GSCALE(60), GSCALE(60));
    _reverseBtn.centerX=self.view.centerX;
    _reverseBtn.bottom=_reverselabel.top-GSCALE(36);
    _reverseBtn.top=_reverseImageView.top+GSCALE(30);
    
    _reverselabel.size=CGSizeMake(GSCALE(142), GSCALE(17));
    _reverselabel.centerX=self.view.centerX;
    _reverselabel.top=_reverseBtn.bottom+GSCALE(32);
    }
    
    _submit.layer.cornerRadius=20.f;
    _submit.size=CGSizeMake(GSCALE(300), GSCALE(44));
    _submit.top=_reverseImageView.bottom +GSCALE(20);
    [_submit addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside ];
    
}

- (IBAction)licenseBtn:(id)sender {
    [self selectPhoto:@"0"];
}
- (IBAction)frontBtn:(id)sender {
    [self selectPhoto:@"1"];
}
- (IBAction)reverseBtn:(id)sender {
    [self selectPhoto:@"2"];
}
-(void)submitClick:(UIButton *)btn{
    if (self.type==0) {
        img1=@"";
        if(img3==0||img2==0){
            [self presentMessageTips:NSLocalizedString(@"Please Upload ID", nil)];
            return;
        }
    }else if(self.type==1){
          if(img3==0||img2==0||img1==0){
//           [self presentMessageTips:@"请上传证件"];
             [self presentMessageTips:NSLocalizedString(@"Please Upload ID", nil)];
             return;
          }
    }
    __weak typeof(self) weakSelf = self;
    self.identyApi=[[IGIdentityAuthenApi alloc]initWithUserType:self.userType certificateType:self.certificatType name:self.name certificateCode:self.certificateCode orderImage:img2 reverseImage:img3 businessImage:img1];
    [self.identyApi startWithDecodeClass:[IGIdentityAuthenResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [UIViewController ig_delectViewController:[PhotoUpLoadAgreementViewController class]];
            [UIViewController ig_delectViewController:[UserIdentifyViewController class]];
            [UIViewController ig_delectViewController:[UserUNidentyViewController class]];
//            [weakSelf presentMessageTips:@"上传成功"];
            [weakSelf presentMessageTips:NSLocalizedString(@"Uploaded Successfully", nil)];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
//            [weakSelf presentMessageTips:@"上传失败"];
            [weakSelf presentMessageTips:NSLocalizedString(@"Upload Failed", nil)];
        }
    }];
    
}

- (void)selectPhoto:(NSString *)index {
    @KWeakObj(self)
    [IGPermissionUtil ig_showActionSheetWithCameraBlock:^{
        [IGTakePhotosUtil ig_showImagePickerControllerWithImagePickerBlock:^(NSDictionary<NSString *,id> * _Nullable info) {
            if (info) {
                UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                image = [image fixOrientation];
                [selfWeak uploadToServer:image index:index];
            }
        }];
    } photoBlock:^{
        [IGTakePhotosUtil ig_showAssetsPickerControllerWithMaximumNumberOfSelection:1 assetsPickerBlock:^(NSArray<UIImage *> * _Nullable assets) {
            if (assets.count != 0) {
                UIImage *image = assets[0];
                [selfWeak uploadToServer:image index:index];
            }
        }];
    }];
}

- (void)uploadToServer:(UIImage *)photo index:(NSString *)index {
//    [self showText:@"正在上传..."];
    [self showText:NSLocalizedString(@"Uploading", nil)];
    @KWeakObj(self)
    self.zhengImageApi = [[IGUploadImageApi alloc]initWithImage:photo];
    [_zhengImageApi startWithDecodeClass:[IGUploadImageResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGUploadImageResp *resp=responseObject;
            IGUploadImageModel *modle=resp.data;
            if (self.type==0) {
                if ([index integerValue] == 1) {
                    img2 = modle.imageName;
                    selfWeak.frontImageView.image = photo;
                } else if ([index integerValue] == 2) {
                    img3 = modle.imageName;
                    selfWeak.reverseImageView.image = photo;
                }
            }else{
                if ([index integerValue] == 0) {
                   img1 = modle.imageName;
                   selfWeak.licenseImageView.image = photo;
                } else if ([index integerValue] == 1) {
                   img2 = modle.imageName;
                   selfWeak.frontImageView.image = photo;
                } else if ([index integerValue] == 2) {
                   img3 = modle.imageName;
                   selfWeak.reverseImageView.image = photo;
                }
            }
            [selfWeak dismissLoading];
        } else {
//            [selfWeak presentMessageTips:@"上传失败"];
            [selfWeak presentMessageTips:NSLocalizedString(@"Upload Failed", nil)];

        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
